<template>
  <el-dialog title="上传纵断面图纸" :visible.sync="dialogVisible" width="25%" :before-close="handleClose">
    <el-form ref="form" :model="form" label-width="100px">
      <el-form-item label="隧道">
        <el-select v-model="form.tId" placeholder="请选择隧道">
          <el-option
            command="e"
            v-for="v in tunnelList"
            :label="v.tunName"
            :key="v.id"
            :value="v.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="图号">
        <el-input v-model="form.picNumber"></el-input>
      </el-form-item>
      <el-form-item label="图名">
        <el-input v-model="form.picName"></el-input>
      </el-form-item>
      <el-form-item label="图纸">
        <el-upload
          name="file"
          class="upload-demo"
          action="string"
          :http-request="beforeAvatarUploadThree"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :before-remove="beforeRemove"
          multiple
          :limit="3"
          :on-success="handlesuccess"
          :on-error="handleerror"
          :on-exceed="handleExceed"
          :file-list="fileList"
        >
          <el-button size="small" type="primary">点击上传</el-button>
        </el-upload>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="create">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import api from '../../../../api'
import qs from 'qs'

export default {
  data() {
    return {
      dialogVisible: false,
      tunnelList: [],
      form: {
        tId: '', //隧道id
        picNumber: '', //图号
        picName: '', // 图名
        resources: '' // 资源地址
      },
      fileList: []
    }
  },
  mounted() {
    this.$bus.$on('onAddVertical', (flag) => {
      this.dialogVisible = flag
    })
    this.findByTunnelInfo()

  },
  methods: {
    findByTunnelInfo(){
      // 查询隧道
      api
        .tunnelInfoFindByAll()
        .then((res) => {
          if (res.data.code == 2001) {
            this.tunnelList = res.data.data
          }
        })
        .catch((err) => {
          console.log(err)
        })
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          done()
        })
        .catch((_) => {})
    },
    create() {
      api
        .drawVerticalsave(qs.stringify(this.form))
        .then((res) => {
          if (res.data.code == 2001) {
            this.$message({
              type: 'success',
              message: '添加成功!'
            })
            this.dialogVisible = false
            this.$bus.$emit('onRefreshVertical', true)
          }
        })
        .catch((err) => {})
    },
    onSubmit() {
      console.log('submit!')
    },
    uploadImg(f) {
      const file = f.file
      const d = new FormData()
      d.append('file', file)
      api
        .AddrPlaneFileUpload(d)
        .then((res) => {
          if (res.data.code == 2001) {
            this.form.resources = res.data.data
          }
        })
        .catch((err) => {})
    },
    // 上传图片前的过滤
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 1000
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 200MB!')
      }
      return isLt2M
    },
    handlesuccess(response, file, fileList) {
      console.log(response)
      if (response.code == 2001) {
        this.form.resources = response.data
      }
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePreview(file) {
      console.log(file)
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`
      )
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`)
    },
    handleerror(err, file, fileList) {
      console.log(err)
    },
    beforeAvatarUploadThree(file) {
      let params = new FormData()
      params.append('file', file.file)
      api
        .AddrPlaneFileUpload(params)
        .then((res) => {
          if (res.data.code == 2001) {
            this.form.resources = res.data.data
          }
        })
        .catch((err) => {})
    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  position: relative;
  overflow: hidden;
  cursor: pointer;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  width: 50px !important;
  height: 50px !important;
  font-size: 28px;
  line-height: 50px !important;
  color: #8c939d;
  text-align: center;
}
.avatar {
  display: block;
  width: 50px !important;
  height: 50px !important;
}
.imgbeforepad {
  padding-top: 0px !important;
}
.avatar-uploader .el-icon-plus {
  margin: 15px !important;
}
</style>